package pl.com.kyhu.dietex3000.GUI.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import pl.com.kyhu.dietex3000.algorithm.StructuralAlgorithm;

public class ProgressBarDialog extends Dialog {

	protected Object result;
	protected Shell shell;

	ProgressBar progressBar;

	public ProgressBarDialog(Shell parent, int style) {
		super(parent, SWT.DIALOG_TRIM);
		setText("Progress");
	}

	public Object open(StructuralAlgorithm alg) {

		createContents();

		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		alg.run();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		return result;
	}

	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(348, 82);
		shell.setText(getText());
		FillLayout fl_shell = new FillLayout(SWT.HORIZONTAL);
		fl_shell.marginWidth = 5;
		fl_shell.marginHeight = 5;
		shell.setLayout(fl_shell);

		progressBar = new ProgressBar(shell, SWT.NONE);

	}

	public void setSelection(Integer selection) {
		progressBar.setSelection(selection);
	}

	public void setRange(Integer min, Integer max) {
		progressBar.setMinimum(min);
		progressBar.setMaximum(max);
	}

	public void close() {
		shell.dispose();
	}

}
